package com.jinke.uang.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinke.uang.R;
import com.jinke.uang.base.BaseRecyclerAdapter;
import com.jinke.uang.bean.TimeProductData;

import androidx.recyclerview.widget.RecyclerView;

/**
 * @author lixiaonan
 * 功能描述: 产品时间选择
 * 时 间： 2019/4/2 下午10:16
 */
public class ProductTermAdapter extends BaseRecyclerAdapter<TimeProductData, ProductTermAdapter
        .ProductTermViewHolder> {

    private OnProductTermListener listener;
    private int mSelectItem = 0;
    private Context context;

    public ProductTermAdapter(Context mcontext, OnProductTermListener listener) {
        super(mcontext);
        context=mcontext;
        this.listener = listener;
    }

    public int getSelect() {
        return mSelectItem;
    }

    @Override
    public ProductTermViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new ProductTermViewHolder(inflater.inflate(R.layout
                .viewholder_product_term_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final ProductTermViewHolder viewHolder, final int
            position) {
        viewHolder.tvContent.setText(mDatas.get(position).getPTerm());
        if("1".equals(mDatas.get(position).getStatus())){
            if (mSelectItem >= 0 && mSelectItem == position) {
                viewHolder.tvContent.setSelected(true);
            } else {
                viewHolder.tvContent.setSelected(false);
            }
        }else{
            viewHolder.tvContent.setBackgroundResource(R.drawable.home_product_noclick_bg);
            viewHolder.tvContent.setTextColor(context.getResources().getColor(R.color.home_product_noclick));
        }

        viewHolder.tvContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == mSelectItem) {
                    return;
                } else {
                    mSelectItem = position;
                    notifyDataSetChanged();
                    if (listener != null) {
                        listener.onProductTermClick(view, position);
                    }
                }

            }
        });
    }

    public class ProductTermViewHolder extends RecyclerView.ViewHolder {
        TextView tvContent;

        public ProductTermViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
        }
    }

    public interface OnProductTermListener {
        void onProductTermClick(View view, int position);
    }
}
